package com.lyhlmj.oop_client.Utils;

import com.lyhlmj.oop_client.ClientOperation.ClientUtil.Client;
import com.lyhlmj.oop_client.Frames.WaitFrame.WaitFrame;
import org.json.JSONObject;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//退出系统操作
public class ExitUtil {
    private static Client client = Client.client;

    //用户登陆之前退出系统
    public static void exitBeforeLogin(JFrame frame) {
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 设置关闭窗口时不进行操作
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int flag = JOptionPane.showConfirmDialog(frame, "是否确认退出系统?",
                        "提示!", JOptionPane.YES_NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE);
                if (JOptionPane.YES_OPTION == flag) {
                    System.exit(0);
                } else {
                    return;
                }
            }
        });
    }

    //用户登陆后，但是没有开始对局，此时退出登陆
    public static void exitAfterLogin(JFrame frame) {
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 设置关闭窗口时不进行操作
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int flag = JOptionPane.showConfirmDialog(frame, "是否确认退出游戏?",
                        "提示!", JOptionPane.YES_NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE);

                //用户选择确认
                if (JOptionPane.YES_OPTION == flag) {
                    //仅需要登出用户即可
                    logout(client);
                    client.getConnection().close(); // 关闭客户端//关闭获取对局状态的线程
                    System.exit(0);  //退出系统
                } else {
                    return;
                }
            }
        });
    }

    //用户在对局中退出游戏（认输）
    public static void exitInPlay(JFrame frame) {
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 设置关闭窗口时不进行操作
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int flag = JOptionPane.showConfirmDialog(frame, "您正在进行游戏，现在退出相当于认输。您是否确定退出游戏？",
                        "提示!", JOptionPane.YES_NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE);

                //用户选择确认
                if (JOptionPane.YES_OPTION == flag) {
                    //向后端发送认输请求
                    JSONObject jsonReq = Request.getJsonObject("concede", "username", client.getUsername(), "chatRoomId", client.getChatRoomId());
                    client.send(jsonReq, new Client.Callback() {
                        @Override
                        public void onResult(int status, String reason, JSONObject data) {
                            if (status == 0) {
                                JOptionPane.showMessageDialog(frame, reason);
                            }
                        }
                    });

                    //关闭对局相关的线程
                    Client.client.getPlayStatusTask.doStop();//关闭获取游戏状态的线程
                    Client.client.getChatInfoTask.doStop(); // 控制消息接收线程，令其退出

                    //删除client内部的用户对局信息
                    Client.client.setChatRoomId("");
                    Client.client.setPlayers(null);
                    Client.client.setPlayId(0);
                    Client.client.getChatInfoTask = null;
                    Client.client.getPlayStatusTask = null;

                    //关闭当前界面，回到匹配界面，用户不登出，可以重新匹配
                    frame.setVisible(false);
                    WaitFrame.initial();
                } else {
                    return;
                }
            }
        });
    }

    //用户获胜后退出游戏
    public static void exitAfterWin(JFrame frame) {
        //关闭对局相关的线程
        Client.client.getPlayStatusTask.doStop();//关闭获取游戏状态的线程
        Client.client.getChatInfoTask.doStop(); // 控制消息接收线程，令其退出

        //删除client内部的用户对局信息
        Client.client.setChatRoomId("");
        Client.client.setPlayers(null);
        Client.client.setPlayId(0);
        Client.client.getChatInfoTask = null;
        Client.client.getPlayStatusTask = null;

        //关闭当前界面，回到匹配界面，用户不登出，可以重新匹配
        frame.setVisible(false);
        WaitFrame.initial();
    }

    //（通用方法）用户退出登陆的操作
    public static void logout(Client client) {
//        向后端发送登出信号
        JSONObject jsonReq = Request.getJsonObject("logout", "username", client.getUsername(), "chatRoomId", client.getChatRoomId());
        try {
            client.send(jsonReq);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("登出过程出现错误！\n");
        }
    }
}
